//define(['js/module.js'], function(controllers) {
	'use strict';
	angular.module('backcenter.rgSelectMulti', [])
	.controller('orgSelectMultiCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , function($rootScope, $scope, BizData, $uibModalInstance, params, title
			   ) {

		var Model = this;
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataMasterIndexChanged = function(event) {

		}
		
		/**
		name:bizData#onAfterRefresh
		description: <b>[回调型事件]</b>业务数据刷新后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象
			"limit" : 页大小, 
			"offset" : 偏移,
			"success" : 是否成功刷新
		}
		</xmp>	
		*/
		Model.dataMasterAfterRefresh = function(event){
			var dataTree = event.source;
			//展开第一个
			var rowID = dataTree.getCurrentID();
			if(rowID!=null){
				dataTree.expandRow(rowID);
			}
		}

		var options = {};
		options.container = Model;
		options.concept = 'SaOporg';
		options.name = 'dataOrg';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/backcenter/org/tree/create';
		options.reader = '/backcenter/org/tree/query';
		options.writer = '/backcenter/org/tree/save';
		options.deleter = '/backcenter/org/tree/delete';
		options.idColumnDefine = "sID";
		options.idColumnName = "sID";
		options.isTree = true;
		options.parentRelation = 'sParent';
		options.nodeKindRelation = 'sNodeKind';
		options.nodeLevelRelation = 'sLevel';
		options.nodeSortRelation = "sSequence";
		options.rootFilter = 'SaOporg.sParent is null';
		options.pathRelation = 'sFID';
		options.directDeleteMode = false;
   		options.limit = -1;
		options.orderBy = 'sSequence ASC';
		
		options.onIndexChanged = Model.dataMasterIndexChanged;
		options.onAfterRefresh = Model.dataMasterAfterRefresh;
		
		$scope.dataMaster = new BizData(options);

		//监视对象中的所有变动
		$scope.dataMaster.deregRows = $scope.$watch('dataMaster.rows',$scope.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
    		Model.title = title;  
    		Model.params = params;  

    		//设置过滤条件
    		$scope.dataMaster.filter = "SaOporg.sFID like '%" + Model.params.id + "%'";
//    		$scope.dataMaster.filter = "SysInfoOrg.orgID='"+ Model.params.id +"'";//+" AND sOrgKindID='ogn'";
//    		$scope.dataMaster.filter = "SysInfo.fID IN (SELECT SysInfoOrg.systemID FROM SysInfoOrg SysInfoOrg WHERE SysInfoOrg.enterpriseNo='"+ Model.params.id +"')";//+" AND sOrgKindID='ogn'";
    		//刷新数据
    	    $scope.dataMaster.refreshData();

        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
			for(var index=0;index<$scope.dataMaster.rows.length;index++){
				if($scope.dataMaster.rows[index].userdata.checked){
			    	
			    	var sourceRow = $scope.dataMaster.rows[index];
			    	var row = {};
					//循环行中的列，判断变化点是哪一个
					for(var obj in sourceRow){
						if(obj!='$$hashKey' && obj!='userdata'){
							row[obj] = sourceRow[obj].value;
						}
					}
			    	
			    	data.push(row);
					
				}
			}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
	    
	    //表格标题中的选择对话框ng-model
	    $scope.checkboxValue = false;
	    //表格标题中的选择对话框click事件
	    Model.checkboxClick = function () {  
			for(var index=0;index<$scope.dataMaster.rows.length;index++){
				$scope.dataMaster.rows[index].userdata.checked=$scope.checkboxValue;
			}
	    };    
		//////////////////////对话框使用结束//////////////////////////////////////


		
		
	    return Model;

	    
	    
	}])//控制器结束
	
	
//});
